unit editframesform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  ComCtrls, StdCtrls, nkTitleBar, BCLabel, BCButton;

type

  { TfrmEditFrames }

  TfrmEditFrames = class(TForm)
    BCButton2: TBCButton;
    BCButton3: TBCButton;
    BCButton4: TBCButton;
    BCButton5: TBCButton;
    BCLabel1: TBCLabel;
    Label1: TLabel;
    ls: TListView;
    nkTitleBar1: TnkTitleBar;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure BCButton3Click(Sender: TObject);
    procedure BCButton4Click(Sender: TObject);
    procedure BCButton5Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private

  public
    procedure FillList;
  end;

var
  frmEditFrames: TfrmEditFrames;

implementation

uses
  datamodule, editframeform;

{$R *.frm}

{ TfrmEditFrames }

procedure TfrmEditFrames.BCLabel1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmEditFrames.BCButton3Click(Sender: TObject);
begin
  //新增图板
  frmEditFrame:=TfrmEditFrame.Create(Application);
  frmEditFrame.IsNew:=True;
  if frmEditFrame.ShowModal=mrOK then
  begin
    //增加到数据库
    with dm.pubqry do
    begin
      Close;
      SQl.Clear;
      Sql.Add('insert into frames (fname, fsrc) values (:fname, :fsrc)');
      Params.ParamByName('fname').AsString:=Trim(frmEditFrame.Edit1.Text);
      Params.ParamByName('fsrc').AsString:=Trim(frmEditFrame.Edit2.Text);
      ExecSQL;
    end;
    FillList;
  end;
  frmEditFrame.Free;
  frmEditFrame:=nil;
end;

procedure TfrmEditFrames.BCButton4Click(Sender: TObject);
var
  mid:integer;
begin
  //删除
  if ls.Selected=nil then
  begin
    MessageDlg('请选择要编辑的项目。',mtWarning,[mbOK],0);
    Exit;
  end;
  mid:=strtoint(ls.Selected.SubItems.Strings[1]);
  if MessageDlg('确定要删除选中的项目吗？',mtConfirmation,[mbYes,mbNo],0)=mrYes then
  begin
    with dm.pubqry do
    begin
      Close;
      Sql.Clear;
      Sql.Add('delete from frames where id=:id');
      Params.ParamByName('id').AsInteger:=mId;
      ExecSQL;
    end;
    FillList;
  end;
end;

procedure TfrmEditFrames.BCButton5Click(Sender: TObject);
var
  mid:integer;
begin
  //编辑图板
  if ls.Selected=nil then
  begin
    MessageDlg('请选择要编辑的项目。',mtWarning,[mbOK],0);
    Exit;
  end;
  mid:=strtoint(ls.Selected.SubItems.Strings[1]);
  frmEditFrame:=TfrmEditFrame.Create(Application);
  frmEditFrame.IsNew:=False;
  frmEditFrame.Edit1.Text:=ls.Selected.Caption;
  frmEditFrame.Edit2.Text:=ls.Selected.SubItems.Strings[0];
  if frmEditFrame.ShowModal=mrOK then
  begin
    with dm.pubqry do
    begin
      Close;
      Sql.Clear;
      Sql.Add('update frames set fname=:fname, fsrc=:fsrc where id=:id');
      Params.ParamByName('fname').AsString:=Trim(frmEditFrame.Edit1.Text);
      Params.ParamByName('fsrc').AsString:=Trim(frmEditFrame.Edit2.Text);
      Params.ParamByName('id').AsInteger:=mId;
      ExecSQL;
    end;
    FillList;
  end;
end;

procedure TfrmEditFrames.FormShow(Sender: TObject);
begin
  //填充列表
  FillList;
end;

procedure TfrmEditFrames.FillList;
begin
  ls.Items.BeginUpdate;
  ls.Items.Clear;
  with dm.pubqry do
  begin
    Close;
    Sql.Clear;
    Sql.Add('select * from frames where ws="global" order by id');
    Open;
    First;
    While not Eof do
    begin
      with ls.Items.Add do
      begin
        Caption:=FieldByName('fname').AsString;
        SubItems.Add(FieldByName('fsrc').AsString);
        SubItems.Add(inttostr(FieldByName('id').AsInteger));
      end;
      Next;
    end;
  end;
  ls.Items.EndUpdate;
end;

end.

